package com.ctbri.manage.file.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss.s", "yyyy-MM-dd HH:mm:ss",
			"yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };

	public static final String DATE_FORMAT_CN = "yyyy年MM月dd日";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String DATE_FORMAT_NO_DELIMITER = "yyyyMMdd";
	public static final String TIME_FORMAT = "HH:mm:ss";
	public static final String TIME_FORMAT_NO_SEC = "HH:mm";
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.S";
	public static final String DATE_TIME_FORMAT_NO_SEC = "yyyy-MM-dd HH:mm";
	public static final String DATE_TIME_FORMAT_NO_DELIMITER = "yyyyMMddHHmmss";
	public static final String DATE_FORMAT_NO_DAY = "yyyy-MM";
	public static final String DATE_FORMAT_MONTHDAY = "MMdd";
	public static final String DATE_TIME_FORMAT_SEP = "yyyy-MM-dd-HH-mm-ss";
	public static final String DATE_TIME_FORMAT_HHmm = "yyyy-MM-dd HH:mm";

	public static final String DATE_TIME_MONGO_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_TIME_ECODE_FORMAT = "yyyy-MM-dd-HH-mm-ss";
	public static final String DATE_TIME_KEY_FORMAT = "yyyyMMdd";
	/**
	 * 获取当前时间的前一天时间
	 * @return
	 */
	public static Date getBeforeOneDayTime(){
		Date date=new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		return date;
	}

	/**
	 * 将string格式的字符串转化为Date类型的
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getConvertDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
			Date date = sdf.parse(dateString);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 两个日期中间的 天数
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<String> GetBetweenDays(String start, String end) {

		List<String> result = new ArrayList<String>();

		if (start.equals(end)) {
			result.add(start);
			return result;
		}

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Date dateStart = sdf.parse(start);
			Date dateEnd = sdf.parse(end);
			Date tmp = dateStart;

			while (tmp.getTime() < dateEnd.getTime()) {
				result.add(sdf.format(tmp));
				tmp.setTime(tmp.getTime() + 24 * 3600 * 1000);
			}
			result.add(sdf.format(dateEnd));

		} catch (Exception ex) {

		}
		return result;
	}
	
	/**
	字符串转日期类型
	*/
   public static Date getHHmmConvertDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_HHmm);
			Date date = sdf.parse(dateString);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 当前准时日期
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
   /**
	 得到当前日期字符串 格式（yyyy-MM-dd） 
	 pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
		public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	/**
	 得到日期字符串 默认格式（yyyy-MM-dd） 
	 pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}
	/**
	 得到日期时间字符串
	 转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	
	
	
	
}